class ContactManager:
    def __int__(self):
        self.contacts = {}

    def add_contact(self,name,phone):

        if name in self.contacts:
            print(f"[警告]{name} 已存在, 当前电话: {self.contacts[name]}")
            if input("是否更新? (y/n): ").lower() =='y':
                self.contacts[name] =phone
                print("更新成功! ")

        else:
            self.contacts[name] = phone
            print(f"已添加: {name} - {phone}")

    def delete_contact(self,name):
        """"删除联系人"""
        if name in self.contacts:
            print(f"删除: {name} - {self.contacts.pop(name)}")
        else:
            print(f"[错误] 未找到联系人: {name}")
    def search_contact(self,name):
        """查找联系人"""
        phone = self.contacts.get(name,None)
        if phone:
            print(f"找到联系人: {name} - {phone}")
        else:
            print(f"未找到: {name}")
    def show_all(self):
        """显示所有联系人"""
        print("\n=== 通讯录 ===")
        for name, phone in self.contacts.items():
            print(f"{name:<10}: {phone}")
        print(f"共 {len(self.contacts)}个联系人")



manager = ContactManager()
manager.add_contact("张三","13800138000")
manager.add_contact("李四","13912345678")
manager.add_contact("张三","15900000000")



manager.search_contact("李四")
manager.delete_contact("王五")
manager.show_all()
"""
=== 通讯录 ===
张三      : 15900000000
李四      : 13912345678
共 2 个联系人
"""





import re
from collections import Counter

def word_frequency(text,ignore_case=True):
    """统计文本中单词出现频率"""

    if ignore_case:
        text = text.lower()
    words = re.findall(r'\b\w+\b',text)
    return Counter(words)



sample_text = """
Python is powerful, and Python is fast.
It's also easy to learn! python 3.10 released in 2021.
"""



freq = word_frequency(sample_text)
print("单词频率统计:")
for word, count in freq.most_common(5):
    print(f"{word}: {count}次")




stop_words = {"is", "and","it","in","to"}
filtered_freq = {k:v for k,v in freq.items() if k not in stop_words}
print("\n过滤后的高频词:", filtered_freq)




import json


def save_contacts(contacts, filename="contacts.json"):
    with open(filename, 'w') as f:
        json.dump(contacts, f)


def load_contacts(filename="contacts.json"):
    try:
        with open(filename) as f:
            return json.load(f)
    except FileNotFoundError:
        return {}


def save_to_file(self, filename="contacts.josn"):
    save_contacts(self.contacts, filename)

@classmethod
def load_from_file(cls, filename="contacts.json"):
    manager = cls()
    manager.contacts = load_contacts(filename)
    return manager




from wordcloud import WordCloud
import matplotlib.pyplot as plt


def generate_wordcloud(freq_dict):
    wc = WordCloud(width=800, height=400, background_color='white')
    wc.generate_from_frequencies(freq_dict)
    plt.imshow(wc, interpolation='bilinear')
    plt.axis("off")
    plt.show()




generate_wordcloud(freq)


def show_by_group(self):
    groups = {}
    for name in self.contacts:
        initial = name[0].upper()
        groups.setdefault(initial, []).append(name)

    for initial in sorted(groups):
        print(f"\n[{initial}]")
        for name in sorted(groups[initial]):
            print(f"{name}: {self.contacts[name]}")






